<?php


class Login extends CI_Controller
{
	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
		$this->load->model('user_mdl','user');
	}
	
	public function index()
	{
		//$this->session->set_userdata('user',array());
		if($this->auth->hasLogin())
		{
			redirect('index');
		}
		$this->load->library('form_validation');
		$this->form_validation->set_rules('txusername', '用户名', 'required');
		$this->form_validation->set_rules('txpassword', '密码', 'required');
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('login');
		}
		else
		{
			//验证户名和密码
			$user = $this->user->validate_user(
									$this->input->post('txusername', TRUE), 
									$this->input->post('txpassword', TRUE)
								);

			if(!empty($user))
			{
				//更新登录信息
				if($this->auth->process_login($user))
				{
					redirect('index');
				}
			}
			else
			{
				sleep(1);//嘿嘿，谁爆破密码就让谁睡	
				$this->session->set_flashdata('login_error', 'TRUE');	
				$this->load->view('login');
			}
		}
	}
	
	 /**
     * 用户登出wrapper
     * 
     * @access public
     * @return void
     */
	public function logout()
	{
		$this->auth->process_logout();
	}
}